package com.cetc;

import org.junit.Test;

import java.io.*;

/**
 * @Classname IOInputStreamReaderWriterTest
 * @Description 转换流的使用
 * InputStreamReader：将一个字节的输入流转换为字符的输入流
 * OutputStreamWriter：将一个字符的输出流转换为字节的输出流
 * @Date 2022/11/25 09:25
 * @Created by louzhuangxi
 */
public class IOInputStreamReaderWriterTest {
    /**
     * 测试字节输入流-》字符输入流-》字节输出流的转换
     *
     * @throws IOException
     */
    @Test
    public void test01() throws IOException {
        FileInputStream fis = new FileInputStream("data/hello.txt");
        FileOutputStream fos = new FileOutputStream("data/hello8_gbk.txt");
        InputStreamReader isr = new InputStreamReader(fis, "utf-8");
        OutputStreamWriter osw = new OutputStreamWriter(fos, "gbk");
        int len;
        char[] cBuf = new char[4];
        while ((len = isr.read(cBuf)) != -1) {
            String data = new String(cBuf, 0, len);
            osw.write(data);
        }
        isr.close();
        osw.close();
    }


}
